package cn.antho.basic.util;

import cn.antho.pet.domain.Pet;
import cn.antho.pet.service.IPetService;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Value;
import cn.antho.basic.config.SpringUtil;
import org.springframework.context.ApplicationContext;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import java.io.File;
import java.io.FileWriter;

public class FreemarkHtml {

    // 注入freemarker核心对象
//    @Autowired
//    private FreeMarkerConfigurer freeMarkerConfigurer;

    // 获取自定义的模板生成路径
//    @Value("${pagedir}")
//    private static String path;

//    @Autowired
//    private IPetService petService;

    public static void staticHtml(String path, Object obj, Long id) throws Exception {
        ApplicationContext context = SpringUtil.getContext();
        FreeMarkerConfigurer freeMarkerConfigurer = context.getBean(FreeMarkerConfigurer.class);
        IPetService petService = context.getBean(IPetService.class);
        // 1.创建模板技术的核心配置对象
        Configuration configuration = freeMarkerConfigurer.getConfiguration();
        // 2.指定模板名称
        Template template = configuration.getTemplate("freemarker.ftl");
        // 3.准备数据
        // 4.根据数据+模板在指定位置生成静态页面
        FileWriter fileWriter = new FileWriter(new File(path + "\\" + id + ".html"));
        template.process(obj, fileWriter);
        // 关流
        fileWriter.close();
    }
}
